package com.stu.management.controller;


import com.stu.management.pojo.UsersStu;
import com.stu.management.service.UsersService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@SuppressWarnings("all")
@Controller
public class LoginController {
    @Resource(name = "usersServiceImpl")
    private UsersService usersService;

    @RequestMapping("/login.do")
    public void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        //1.接收参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //2.调取service

        UsersStu users = usersService.login(username, password);
        //3.跳转页面
        if(users==null){
            //以弹窗方式提示用户，登录失败
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter writer = resp.getWriter();
            writer.println("<script>location.href='login.jsp';alert('用户名或密码不正确');</script>");
        }else{
            //跳转到主页面
            //保存用户信息
            req.getSession().setAttribute("u1",users);
            System.out.println(users.toString());
            resp.sendRedirect("index.jsp");
        }
    }


    @RequestMapping("/loginout.do")
    public void loginout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        //1.接收参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //2.调取service
        UsersStu users = usersService.login(username, password);
        //3.跳转页面
        if(users==null){
            //以弹窗方式提示用户，登录失败
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter writer = resp.getWriter();
            writer.println("<script>location.href='login.jsp';alert('用户名或密码不正确');</script>");
        }else{
            //跳转到主页面
            //保存用户信息
            req.getSession().setAttribute("u1",users);
            System.out.println(users.toString());
            resp.sendRedirect("index.jsp");
        }
    }

}
